Custom exceptions হল সেই ধরনের exceptions যা আপনি আপনার নিজের প্রয়োজনে তৈরি করেন। Java তে exception handling এর জন্য Exception অথবা RuntimeException ক্লাসগুলির subclass তৈরি করা হয়, যাতে আপনি নির্দিষ্ট সমস্যা বা ত্রুটির জন্য নিজস্ব exception তৈরি করতে পারেন। Custom exceptions প্রোগ্রামিং এর ক্ষেত্রে খুবই গুরুত্বপূর্ণ, কারণ এগুলি আপনাকে নির্দিষ্ট ত্রুটি শর্তাবলী সম্পর্কিত বার্তা বা আচরণ কাস্টমাইজ করতে সহায়ক হয়।
Custom Exception তৈরি করার প্রক্রিয়া:
Java তে custom exception তৈরি করতে, আপনাকে একটি নতুন class তৈরি করতে হবে যা Exception অথবা RuntimeException ক্লাসের সাবক্লাস হবে। সাধারণত, custom exception এ constructor, message, এবং cause মেসেজ হ্যান্ডলিং এর জন্য কাস্টম ফিচার সংযুক্ত করা হয়।
Custom Exception তৈরি করার উদাহরণ (Checked Exception):
// Custom checked exception by extending Exception
public class InvalidAgeException extends Exception {
// Default constructor
public InvalidAgeException() {
super("Invalid age provided.");
}
// Constructor that accepts a custom error message
public InvalidAgeException(String message) {
super(message);
}
}
Explanation:
InvalidAgeExceptionহল একটি custom exception যাExceptionক্লাস থেকে ইনহেরিট করা হয়েছে।- এতে একটি ডিফল্ট কনস্ট্রাক্টর এবং একটি কাস্টম মেসেজ প্যারামিটার সহ কনস্ট্রাক্টর রয়েছে।
Usage Example:
import java.util.Scanner;
public class TestCustomException {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
try {
if (age < 18) {
// Throwing the custom exception if age is less than 18
throw new InvalidAgeException("Age must be 18 or older.");
}
System.out.println("Valid age.");
} catch (InvalidAgeException e) {
// Catching and handling the custom exception
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
Output:
Enter age: 15
Caught Exception: Age must be 18 or older.
Custom Exception (Unchecked Exception):
এছাড়া, আপনি Unchecked Exceptions তৈরি করতে RuntimeException ক্লাস থেকেও ইনহেরিট করতে পারেন। Unchecked exceptions সাধারণত runtime errors বা logic errors কে প্রতিনিধিত্ব করে এবং এটি try-catch ব্লকে handle করার বাধ্যবাধকতা নেই।
Custom Unchecked Exception Example:
// Custom unchecked exception by extending RuntimeException
public class InvalidNumberException extends RuntimeException {
// Default constructor
public InvalidNumberException() {
super("Invalid number input.");
}
// Constructor that accepts a custom error message
public InvalidNumberException(String message) {
super(message);
}
}
Explanation:
InvalidNumberExceptionহল একটি unchecked exception যাRuntimeExceptionথেকে ইনহেরিট করা হয়েছে।- এতে একটি ডিফল্ট কনস্ট্রাক্টর এবং একটি কাস্টম মেসেজ প্যারামিটার সহ কনস্ট্রাক্টর রয়েছে।
Usage Example:
import java.util.Scanner;
public class TestUncheckedCustomException {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive number: ");
int number = scanner.nextInt();
if (number < 0) {
// Throwing the custom unchecked exception
throw new InvalidNumberException("Number cannot be negative.");
}
System.out.println("Valid number entered: " + number);
}
}
Output:
Enter a positive number: -5
Exception in thread "main" InvalidNumberException: Number cannot be negative.
at TestUncheckedCustomException.main(TestUncheckedCustomException.java:9)
Custom Exception কাস্টমাইজেশন (Additional Methods and Fields):
আপনি চাইলে আপনার custom exception এ আরও কাস্টম ফিল্ড বা মেথড যোগ করতে পারেন, যেমন:
- Error codes: নির্দিষ্ট ত্রুটির কোড প্রদান।
- Additional data: কাস্টম exception এর সাথে আরও ডাটা যুক্ত করা (যেমন, error time বা stack trace তথ্য)।
Custom Exception with Additional Fields:
public class InsufficientBalanceException extends Exception {
private double balance;
// Constructor with message and balance
public InsufficientBalanceException(String message, double balance) {
super(message);
this.balance = balance;
}
// Method to get the balance at the time of exception
public double getBalance() {
return balance;
}
}
Usage Example:
public class TestCustomExceptionWithData {
public static void main(String[] args) {
double accountBalance = 5000.0;
double withdrawalAmount = 6000.0;
try {
if (withdrawalAmount > accountBalance) {
throw new InsufficientBalanceException("Insufficient balance for withdrawal.", accountBalance);
}
System.out.println("Withdrawal successful.");
} catch (InsufficientBalanceException e) {
System.out.println(e.getMessage() + " Current balance: " + e.getBalance());
}
}
}
Output:
Insufficient balance for withdrawal. Current balance: 5000.0
- Custom Exceptions Java তে বিশেষ ত্রুটি হ্যান্ডলিং এর জন্য খুবই কার্যকরী। আপনি checked exceptions বা unchecked exceptions তৈরি করতে পারেন, যা আপনার অ্যাপ্লিকেশনের নির্দিষ্ট সমস্যাগুলির জন্য উপযোগী হবে।
- Checked exceptions এমন ত্রুটির জন্য ব্যবহার করা হয় যেগুলি প্রোগ্রামটি কম্পাইল করার সময় ধরতে হয়, যেমন ফাইল খোলার সময় কোনো সমস্যা হলে।
- Unchecked exceptions সাধারণত runtime ত্রুটির জন্য ব্যবহৃত হয়, যেমন ডিভাইসের ইনপুট সমস্যা বা অ্যারের সীমার বাইরে অ্যাক্সেস।
Java তে custom exceptions তৈরি করার মাধ্যমে আপনি আপনার অ্যাপ্লিকেশন আরও robust, readable, এবং রক্ষণাবেক্ষণযোগ্য করতে পারেন।
Read more